﻿if (typeof Yb == 'undefined') {
    var Yb = {};
}
if (typeof Json == 'undefined') {
    var Json = {};
}
TimeSpan = function () {
    this.Days = 0;
    this.Hours = 0;
    this.Minutes = 0;
    this.Seconds = 0;
    this.ToHours = function (flag) {
        var dayHours = 24;
        if (typeof flag != 'undefined' && flag) {
            dayHours = 8;
        }
        return this.Days * dayHours + this.Hours + this.Minutes / 60;
    }
}

if (typeof Json.Parse !== 'function') {
    Json.Parse = function (obj) {
        var sJson = "";
        for (var item in obj) {
            var date = obj[item];
            if (typeof date != 'function' && typeof date != 'object') {
                if (typeof date != 'undefined' && date != null && typeof date.indexOf != 'undefined' && date.indexOf('Date(') >= 0)
                    sJson += '"' + item + '":"' + FormatDateTime(date, "yyyy-MM-dd hh:mm:ss") + '",';
                else
                    sJson += '"' + item + '":"' + obj[item] + '",';
            }
        }
        sJson = sJson.Trim(',').replace(/null/g, '');
        return JSON.parse('{' + sJson + '}');
    }
}

function NewGuid() {
    return NewID();
}
function NewID() {
    var guid = "";
    for (var i = 1; i <= 32; i++) {
        var n = Math.floor(Math.random() * 16.0).toString(16);
        guid += n;
        if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
            guid += "-";
    }
    return guid;
}

function CheckNotNull(p) {
    return (typeof p != 'undefined' && p != null && p != '')
}

function CheckNull(p) {
    return (typeof p == 'undefined' || p == null || p == '')
}

String.prototype.Trim = function (value) {
    if (CheckNotNull(value) && value.length > 0) {
        var str = this;
        if (str.length >= value.length) {
            if (str.substr(0, value.length) == value) {
                str = str.substring(value.length);
            }
            if (str.length >= value.length) {
                if (str.substr(str.length - value.length, value.length) == value) {
                    str = str.substring(0, str.length - value.length);
                }
            }
        }
    }
    else {
        var str = this, str = str.replace(/^\s\s*/, ''), ws = /\s/, i = str.length;
        while (ws.test(str.charAt(--i)));
        str = str.slice(0, i + 1);
    }
    return str;
}
String.prototype.LTrim = function () {
    return this.replace(/(^\s*)/g, "");
}
String.prototype.Rtrim = function () {
    return this.replace(/(\s*$)/g, "");
}
String.prototype.endWith = function (oString) {
    var reg = new RegExp(oString + "$");
    return reg.test(this);
}
String.prototype.ToDate = function (isCheck) {
    var newDate = "";
    arys = this.split('-');
    arys1 = arys[2].split(' ');
    arys2 = arys1[1].split(':');
    if (typeof isCheck != 'undefined' && isCheck) {
        if (arys2[0] > 18) {
            arys2[0] = 18;
            arys2[1] = 00;
            arys2[2] = 00;
        }
        if (arys2[0] < 9) {
            arys2[0] = 9;
            arys2[1] = 0;
            arys2[2] = 0;
        }
    }
    if (typeof arys2 == 'undefined' || arys2 == null || arys2 == '') {
        var newDate = new Date(arys[0], parseInt(arys[1], 10) - 1, arys1[0]);
    }
    else {
        var newDate = new Date(arys[0], parseInt(arys[1], 10) - 1, arys1[0], arys2[0], arys2[1], arys2[2]);
    }
    return newDate;
}
String.prototype.ToError = function () {
    return '<span class="error">' + this + '</span>';
}
Array.prototype.KeyToString = function () {
    var str = "";
    for (var item in this) {
        if (typeof this[item] != 'function')
            str += item + ",";
    }
    str = str.Trim(',');
    return str;
}

Array.prototype.ValueToString = function () {
    var str = "";
    for (var item in this) {
        if (typeof this[item] != 'function')
            str += this[item] + ",";
    }
    str = str.Trim(',');
    return str;
}
Array.prototype.HashForEach = function (method) {
    for (var item in this) {
        if (typeof this[item] != 'function')
            if (method) {
                method(item);
            }
    }
}
Array.prototype.ToJson = function () {
}
Date.prototype.Format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(), //day
        "h+": this.getHours(), //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}

Date.prototype.IsWeekDay = function () {
    day = this.getDay();
    if (day == 0 || day == 6) {
        return true;
    }
    return false;
}

Date.prototype.Diff = function (date, flag) {this
    var days = parseInt((this - date) / 1000 / 60 / 60 / 24); //获取总的天数
    var hours = parseInt((this - date) / 1000 / 60 / 60) % 24;  //获取余下的小时  
    var minutes = parseInt((this - date) / 1000 / 60) % 60;  //获取余下的分钟
    var seconds = parseInt((this - date) / 1000) % 60;  //获取余下的秒
    if (typeof flag != 'undefined' && flag && hours > 8) {
        hours = 8;
    }
//    var tempDate = date;
//    while (tempDate.getTime() <= date.getTime()) {
//        tempDate = addDays(tempDate, 2);
//        if (typeof flag != 'undefined' && flag && tempDate.IsWeekDay() && days > 1) {
//            days--;
//        }
//    }
    var result = new TimeSpan();
    result.Days = days;
    result.Hours = hours;
    result.Minutes = minutes;
    result.Seconds = seconds;
    return result;
}

function addDays(oDate, days) {
    if (days > 0) {
        days = days - 1;
    }
    if (days < 0) {
        days = days + 1;
    }
    var result = new Date(oDate.getTime() + (days * 24 * 60 * 60 * 1000));
    return result;
}

Date.prototype.ToString = function (format, flag) {
    if (typeof format == 'undefined') {
        format = "yyyy-MM-dd hh:mm:ss";
    }
    return this.Format(format);
    //    var month = this.getMonth() + 1;
    //    var day = this.getDate();
    //    var hour = this.getHours();
    //    var mi = this.getMinutes();
    //    var second = this.getSeconds();
    //    //如果月份小于10月则在前面加0
    //    if (month < 10) {
    //        month = "0" + month;
    //    }
    //    //如果日期小于10号则在前面加0
    //    if (day < 10) {
    //        day = "0" + day;
    //    }
    //    if (hour < 10) {
    //        //如果小于9点 设置为9点
    //        if (hour < 9) {
    //            hour = 9;
    //        }
    //        hour = "0" + hour;
    //    }
    //    //如果大于18点，让他等于18点
    //    if (hour > 18) {
    //        hour = 18;
    //    }
    //    if (mi < 10) {
    //        mi = "0" + mi;
    //    }
    //    if (second < 10) {
    //        second = "0" + second;
    //    }
    //    return this.getFullYear() + "-" + month + "-" + day + " " + hour + ":" + mi + ":" + second;
}

function FormatTime(jsonDate) {
    if (typeof jsonDate == 'undefined' || jsonDate == null || jsonDate == '')
        return '';
    jsonDate = jsonDate.split('(')[1].split(')')[0];
    var rDate = new Date(parseInt(jsonDate));
    return rDate.Format("yyyy-MM-dd");
}
function FormatDateTime(jsonDate, format) {
    if (typeof jsonDate == 'undefined' || jsonDate == null || jsonDate == '')
        return '';
    jsonDate = jsonDate.split('(')[1].split(')')[0];
    var rDate = new Date(parseInt(jsonDate));
    return rDate.Format(format);
}
function GetMaxDate(maxID, format) {
    if (typeof format != 'undefined')
        WdatePicker({ maxDate: '#F{$dp.$D(\'' + maxID + '\')}', dateFmt: format });
    else
        WdatePicker({ maxDate: '#F{$dp.$D(\'' + maxID + '\')}' });
}
function GetMinDate(minID, format) {
    if (typeof format != 'undefined')
        WdatePicker({ minDate: '#F{$dp.$D(\'' + minID + '\')}', dateFmt: format });
    else
        WdatePicker({ minDate: '#F{$dp.$D(\'' + minID + '\')}' });
}

//showModalDialog
//showModelessDialog
//http://w3help.org/tests/BX9036/modaldialog.html
function ShowModalDialogs(dialogPage, pageWidth, pageHeight, callBack) {
    var returnVal = window.showModalDialog(dialogPage, window, "dialogWidth:" + pageWidth + "px;dialogHeight:" + pageHeight + "px;help:no;scroll:yes;status;no");
    if (typeof returnVal == 'undefined' || returnVal == null) {
        returnVal = window.returnValue;
    }
    if (typeof callBack != "undefined")
        callBack(returnVal);
}

function ReturnDialog(returnVal) {
    window.parent.returnValue = returnVal;
    window.returnValue = returnVal;
    if (typeof window.dialogArguments != 'undefined') {
        window.dialogArguments.returnValue = returnVal;
    }
    if (typeof window.opener != 'undefined') {
        window.opener.returnValue = returnVal;
    }
    window.close();
}

function GetErrorSpan(msg) {
    return '<span class="error">' + msg + '</span>';
}

/*阻止事件默认行为*/
function prDefault(evt) {
    /*IE浏览器*/
    if ($.browser.msie) {
        evt.keyCode = 0;
        evt.returnValue = false;
    } else {
        evt.preventDefault();
    }
}

/*禁用页面刷新*/
function ForbiddenRefresh(event) {
    /*F5刷新*/
    if (event.keyCode == 116) {
        prDefault(event);
        return false;
    }

    /*ctrl+r or ctrl+n 刷新 */
    if (event.ctrlKey && (event.keyCode == 78 || event.keyCode == 82)) {
        prDefault(event);
        return false;
    }
    /*shitf +f10 右键菜单*/
    if (event.shiftKey && event.keyCode == 121) {
        prDefault(event);
        return false;
    }
}

(function ($) {
    $(function () {
        /*禁用页面右键*/
        $(document).bind("contextmenu", function (event) {
            return false;
        });
    });

})(jQuery);

function SelectAll(gvID, cbAll, cbName) {
    $("#" + gvID).find("input[type='checkbox']").each(function () {
        var cb = $(this);
        if (cb.attr("id").indexOf(cbName) >= 0) {
            if (cbAll.checked)
                cb.attr("checked", true);
            else
                cb.attr("checked", false);
        }
    });
}

function GoEdit() {
    location = "Edit.aspx";
    return false;
}

function MyConfirm() {
    return confirm("确定删除吗？");
}

function SetCookie(name, value, expiry, path, domain, secure) {
    var nameString = name + "=" + value;
    var expiryString = (expiry == null) ? "" : " ;expires = " + expiry.toGMTString();
    var pathString = (path == null) ? "" : " ;path = " + path;
    var domainString = (path == null) ? "" : " ;domain = " + domain;
    var secureString = (secure) ? ";secure" : "";
    document.cookie = nameString + expiryString + pathString + domainString + secureString;
}

function GetCookie(name) {
    var CookieFound = false;
    var start = 0;
    var end = 0;
    var cookieString = document.cookie;
    var i = 0;
    while (i <= cookieString.length) {
        start = i;
        end = start + name.length;
        if (cookieString.substring(start, end) == name) {
            CookieFound = true;
            break;
        }
        i++;
    }
    if (CookieFound) {
        start = end + 1;
        end = cookieString.indexOf(";", start);
        if (end < start)
            end = cookieString.length;
        return unescape(cookieString.substring(start, end));
    }
    return "";
}
function DeleteCookie(name) {
    var expires = new Date();
    expires.setTime(expires.getTime() - 1);
    sc(name, "Delete Cookie", expires);
}


